
local locplayer = ac.player.self
local w,h = 300,300
local ui = class.panel:builder 
{
    x = 283,
    y = 810,
    w = w,
    h = h,
    --show_flag = false,
    alpha = 0.99,
    level = 99,
    --fdf = 'tooltip_backdrop0',
}
ui.title = ui:add_text('|cffffff00天赋表|r',0,5,nil,30,12,5)
--ui:hide()
local normal_icon = [[core/emptyinvslot1.blp]]
normal_icon = ''
local w1,h1 = 70,70
local x1 = (w-w1*4)/2
local y1 = (h-h1*3-ui.title.h)/2
ui.panels = {}
for i=1,12 do
    local x = (i-1)%4 * w1 + x1
    local y = (math.ceil(i/4)-1) * h1 + 30 + y1
    local panel = ui:add_panel(normal_icon,x,y,w1,h1)
    panel.icon = panel:add_button('',0,0,nil,nil,1)
    panel.icon.frame = panel.icon:add_panel('')
    panel.icon.overlay = panel.icon:add_number_panel((w1-36),h1-28,36,28,8)
    panel.icon.overlay:hide()
    
    panel.tfcard = panel:add_panel([[core/bantouming.blp]],0,0,35,20)
    panel.tfcard.text = panel.tfcard:add_text('名称',0,0,panel.tfcard.w+50,nil,8,4)
    --panel.tfcard.text:set_color(0xffffff00)
    panel.icon.index = i
    ui.panels[i] = panel
end

ui.select = ui:add_highlight([[UI\Widgets\Console\Human\CommandButton\human-activebutton.blp]],0,0,w1,h1)
ui.select:hide()

function ui:update()
    local hero = locplayer.hero
    local data = hero and hero:get_data('展示天赋表') or {}
    for i,panel in ipairs(self.panels) do
        local button = panel.icon
        local skill = data[i]
        button.skill = skill
        if skill then
            button.frame:set_normal_image(skill.frame_art)
            button:set_normal_image(skill.art)
            local num = skill.show_num
            if skill.timer_count then
                num = math.floor(skill.timer_count:get_remaining()/1000)
            end
            if num and num~=0 then
                button.overlay.text:set_text(('%0.f'):format(num))
                button.overlay:show()
            else
                button.overlay:hide()
            end
            panel.tfcard.text:set_text(skill.show_class)
            panel.tfcard:show()
        else
            button:set_normal_image('')
            button.frame:set_normal_image('')
            button.overlay:hide()
            panel.tfcard:hide()
        end
    end
end

function ui:on_button_mouse_enter(button)
    local skill = button.skill
    if skill then
        local x,y = button:get_real_position()
        self.select:set_real_position(x,y)
        self.select:show()
        ac.game:notify('游戏-显示天赋介绍',self,skill:get_title(),skill:get_tip(),'top')
    end
end

function ui:on_button_mouse_leave(button)
    self.select:hide()
    ac.game:notify('游戏-关闭天赋介绍')
end


-- ac.game:event '动画-抽卡显示'(function(_,art,point,index)
--     local button = ui.buttons[index]
--     local x,y = button:get_real_position()
--     local target = {x,y,button.w,button.h}
--     local tween = ac.tween('outBack', 3,point,target)
--     ac.game:notify('动画-播放拖动',art,tween)
-- end)

local icons = {}
for a,panel in ipairs(ui.panels) do
    local icon = class.panel.create([[ReplaceableTextures\CommandButtons\BTNAcolyte.blp]],0,0,panel.w,panel.h)
    icon.w_1 = icon.w
    icon.h_1 = icon.h
    icon:hide()

    function icon:move_animation_finish()
        icon:hide()
        icon:set_width(self.w_1)
        icon:set_height(self.h_1)
    end
    function icon:move_animationing(tween)
        icon:set_alpha(1 - tween.clock)
    end
    icons[a] = icon
end

local animation_index = 0
ac.game:event '吞噬动画'(function(_,player,crad,index)
    if player:is_self() then
        local id = animation_index%(#icons) + 1
        local panel = ui.panels[index]
        local icon1 = icons[id]
        local icon2 = ac['吞噬按钮']
        local x,y = panel:get_real_position()
        icon1:set_real_position(x,y)
        icon1:set_normal_image(crad.art)
        icon1:show()
        
        icon1:move_animation({icon2.x + (icon2.w-16)/2,icon2.y + (icon2.h-16)/2,16,16},0.6)
        animation_index = index
    end
    
end)

ac.hardware:event '界面绘制'(function()
    ui:update()
end)

ac.game:notify('游戏-注册显示UI',ui)

return ui